﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Model.WXshopBuiness;
using Xproject.Framework.Core.Repositories;
using Xproject.Framework.Utility.Extensions;

namespace Repository.WXshopBuiness
{
     public static class ProductAttrRep
    {
        public static List<WeiXinProductAttr> GetProductAttrs(this IRepositoryAsync<WeiXinProductAttr> repository, string productId)
        {
            Expression<Func<WeiXinProductAttr, bool>> express = i => 1 == 1;
            express = express.AndAlso(i =>i.ProductId == productId);
            return repository.Query().Where(express).Select().ToList();
        }


        public static WeiXinProductAttrShowModel GetProductAttrsByCategoryType(this IRepositoryAsync<WeiXinProductAttr> repository, string productId,int categoryType)
        {
            string sql = string.Format(@"
                            SELECT  
                                  wpa.productId AS ProductId
                                  ,wpa.AttrId
                                  ,wpa.AttrValueId
                                  ,wpa.Inputvalue
	                              ,cav.AttrGroupName
	                              ,cav.AttrName
	                              ,cav.AttrGroupId
                                  ,cav.AttrValue
                                  ,wpa.IsInput
                              FROM  [WeiXin_ProductAttr] wpa
                              inner join CategoryAttrValues cav
                              on wpa.AttrValueId = cav.AttrValueId
                              inner join CategoryAttrGroups cag
							  on cag.AttrGroupId = cav.AttrGroupId
                            where cag.CategoryType='{1}' and wpa.productId='{0}'
            ", productId,categoryType);


            return repository.SqlQueryT<WeiXinProductAttrShowModel>(sql).FirstOrDefault();
        }

        public static List<WeiXinProductAttrShowModel> GetProductAttrsF(this IRepositoryAsync<WeiXinProductAttr> repository, string productId)
        {
            string sql = string.Format(@"
                            SELECT  
                                  wpa.productId AS ProductId
                                  ,wpa.AttrId
                                  ,wpa.AttrValueId
                                  ,wpa.Inputvalue
	                              ,cav.AttrGroupName
	                              ,cav.AttrName
	                              ,cav.AttrGroupId
                                  ,cav.AttrValue
                                  ,wpa.IsInput
                              FROM  [WeiXin_ProductAttr] wpa
                              inner join CategoryAttrValues cav
                              on wpa.AttrValueId = cav.AttrValueId
                              inner join CategoryAttrGroups cag
							  on cag.AttrGroupId = cav.AttrGroupId
                            where cag.CategoryType=0 and wpa.productId='{0}'
            ", productId);


            return repository.SqlQueryT<WeiXinProductAttrShowModel>(sql).ToList();
        }

    }
}
